package databasetest;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.InetAddress;
import java.sql.Connection;
import java.sql.DriverManager;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class IPaddr extends JInternalFrame {
	
	String driver="oracle.jdbc.driver.OracleDriver";

	String db_user ="hr";
	String db_pwd="hr";
	GuestBookMain gbm;
	Connection conn;
	private JButton btnNewButton;
	private JTextField ipaddr;

	public IPaddr(Connection conn, GuestBookMain gbm) {
		super("IP주소",true,true,true,false);
		this.gbm=gbm;
		this.conn=conn;
		setBounds(100, 100, 368, 70);
		getContentPane().add(getBtnNewButton(), BorderLayout.EAST);
		getContentPane().add(getIpaddr(), BorderLayout.CENTER);
		try{
		InetAddress ia = InetAddress.getLocalHost();
		ipaddr.setText(ia.getHostAddress());
		}catch(Exception e){
			
		}
	}

	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("접속");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					
					String url="jdbc:oracle:thin:@" + ipaddr.getText() + ":1521:xe";
										
					//데이터베이스 연결 테스트
					//1)드라이버로딩
					try{
						Class.forName(driver).newInstance();
						System.out.println("드라이버가 로딩되었습니다.");
						
					//2)Connection 객체 생성
						gbm.conn=DriverManager.getConnection(url, db_user,db_pwd);
						if(gbm.conn!=null){
						JOptionPane.showMessageDialog(null, "데이터베이스가 정상적으로 연결되었습니다.");
						gbm.closeipaddr();
						}
						
						
					}catch(Exception ex){
						ex.printStackTrace();
					}
					
					gbm.openselect();
					gbm.selectG.columnadd();
//					selectG.columnadd();
					
				}
			});
		}
		return btnNewButton;
	}
	private JTextField getIpaddr() {
		if (ipaddr == null) {
			ipaddr = new JTextField();
			ipaddr.setColumns(10);
		}
		return ipaddr;
	}
}
